//
//  PinchGestureViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/28.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxGesture
import RxCocoa
import RxSwift

class PinchGestureViewController: BaseViewController {

    let disposed = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let tapView = UIView(frame: CGRect(x: 0, y: 80, width: 200, height: 200))
        tapView.backgroundColor = UIColor.randomColor()
        self.view.addSubview(tapView)
        
        tapView.centerInSuperView()
        
        let pinch = tapView.rx.pinchGesture().shareReplay(1)
        
        var s = 1.f
        
        pinch.when(.changed)
            .asScale()
            .subscribe(onNext: { (scale, _) in
                tapView.transform = CGAffineTransform(scaleX: scale * s, y: scale * s)
            })
            .disposed(by: disposed)
        
        pinch.when(.ended)
            .asScale()
            .subscribe(onNext: { (scale, _) in
                s *= scale
                print(s)
                print("pinch ended")
            })
            .disposed(by: disposed)
    }


}
